package com.jprocms.module.cms.service.comment;

import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.common.util.object.BeanUtils;
import com.jprocms.module.cms.controller.admin.comment.vo.CommentListReqVO;
import com.jprocms.module.cms.controller.admin.comment.vo.CommentPageReqVO;
import com.jprocms.module.cms.controller.admin.comment.vo.CommentSaveReqVO;
import com.jprocms.module.cms.dal.dataobject.comment.CommentDO;
import com.jprocms.module.cms.dal.dataobject.content.ContentCountDO;
import com.jprocms.module.cms.dal.mysql.comment.CommentMapper;
import com.jprocms.module.cms.dal.mysql.content.ContentCountMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.COMMENT_NOT_EXISTS;

/**
 * 内容评论 Service 实现类
 *
 * @author jprocms
 */
@Service
@Validated
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private ContentCountMapper contentCountMapper;

    @Override
    public Long createComment(CommentSaveReqVO createReqVO) {
        // 插入
        CommentDO comment = BeanUtils.toBean(createReqVO, CommentDO.class);
        commentMapper.insert(comment);
        // 返回
        return comment.getId();
    }

    @Override
    public void updateComment(CommentSaveReqVO updateReqVO) {
        // 校验存在
        validateCommentExists(updateReqVO.getId());
        // 更新
        CommentDO updateObj = BeanUtils.toBean(updateReqVO, CommentDO.class);
        commentMapper.updateById(updateObj);
    }

    @Override
    public void deleteComment(Long id) {
        // 校验存在
        validateCommentExists(id);
        updateContentCommentCount(id, false);
        // 删除
        commentMapper.deleteById(id);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        for (Long id : ids) {
            deleteComment(id);
        }
    }

    public void updateContentCommentCount(Long commentId, boolean add) {
        final CommentDO comment = getComment(commentId);
        ContentCountDO count = contentCountMapper.selectById(comment.getContentId());
        int comments;
        if (!add) {
            comments = count.getComments() - 1;
        } else {
            comments = count.getComments() + 1;
        }
        if (comments < 0) {
            comments = 0;
        }
        count.setComments(comments);
        contentCountMapper.updateById(count);
    }

    private void validateCommentExists(Long id) {
        if (commentMapper.selectById(id) == null) {
            throw exception(COMMENT_NOT_EXISTS);
        }
    }

    @Override
    public CommentDO getComment(Long id) {
        return commentMapper.selectById(id);
    }

    @Override
    public PageResult<CommentDO> getCommentPage(CommentPageReqVO pageReqVO) {
        return commentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CommentDO> getCommentList(CommentListReqVO listReqVO) {
        return commentMapper.selectList(listReqVO);
    }

}